/* $Header: dt_cmds.c,v 2.2 01/10/02 10:29:15 przybyls Exp $ */

/***********************************************************************/
/*                                                                     */
/*                  Copyright Hewlett-Packard Co. 1991                 */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/* MODULE NAME:  dt_cmds.c                                             */
/* ============                                                        */
/*                                                                     */
/* Author:  Lesley Freed                                               */
/*                                                                     */
/* MODULE PURPOSE:  To define the diagnostic type commands.            */
/*                                                                     */
/***********************************************************************/

/*****************************/
/*   GLOBAL INCLUDE FILES    */
/*****************************/

#include <typedefs.h>
#include <p_wkfile.h>
#include <wkfile.h>
#include <ramps.h>
#include <zone_config.h>
#include <zone_status.h>
#include <status.h>
#include <p_status.h>
#include <error.h>
#include <exception.h>
#include <err_handler.h>
#include <config.h>
#include <mio.h>
#include <cmd_set.h>
#include <cmdproc.h>
#include <method.h>
#include <hostutil.h>
#include <sig_types.h>
#include <diag.h>
#include <mux.h>
#include <sig_ui.h>
#include <clock_ui.h>
#include <cmd_tbls.h>
#include <flow.h>
#include <p_ui.h>
#include <zone_ui.h>
#include <duart.h>

#include <pcb_defs.h>
#include <display.h>

#include <oven.h>

#include <iomap.h>
#include <flash.h>
#include <als.h>

/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/


/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/

   INT32 DT_Parm[3];

#pragma SECTION DATA=pfail
   INT32 uptime[5];
   INT32 runs[5];
#pragma SECTION UNDO

/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/


#include <xref.h>


/*****************************/
/*   LOCAL DEFINES           */
/*****************************/

#define MIN(A,B) ( ((A) < (B)) ? (A) : (B) )

/*****************************/
/*   STATIC VARIABLES        */
/*****************************/


/*****************************/
/*   MACROS                  */
/*****************************/



/*****************************/
/*   IMPLEMENT SECTION       */
/*****************************/


/***********************************************************************/
/* FUNCTION: DoDTEO                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Echo command.                                              */
/*                                                                     */
/* RETURNED VALUE: length of response string                           */
/*                                                                     */
/* WARNINGS:                                                           */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

INT16 DoDTEO(CMD_TYPE *p_cmd,BIT8  *p_resp_parm)
{
U_INT16 len;

   len = 0;

   if (p_cmd->parmv[0] == 1)
   {
      len = PutAsciiParam((BIT8 *)"\"", p_resp_parm);
      len += PutAsciiParam((BIT8 *)&(p_cmd->parms[p_cmd->parmv[1]]),
                                     p_resp_parm+len);
      len += PutAsciiParam((BIT8 *)"\"", p_resp_parm+len);
      return len;
   }
   else
   {
      CmdErrorHandler(p_cmd,1,INVALID_PARAM);
      return NO_RESPONSE;
   }
}

/***********************************************************************/
/* END FUNCTION: DoDTEO                                                */
/***********************************************************************/


BIT8 CheckPassword( BIT8 *string )
{
   if ( StrLen( string ) == 2 )
   {
      if ( ( *string++ == '.' ) && ( *string == '.' ) )  return TRUE;
   }
   return FALSE;
}

/***********************************************************************/
/* FUNCTION: DoDTDA                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Execute the DAC to mux'ed ADC loop back test.              */
/*                                                                     */
/* RETURNED VALUE: length of the response string                       */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:  GetDiagStatus, PutExceptionParam, FreeIODiagResource        */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 PutExceptionParam( EXCEPTION flag, BIT8 *string )
{
   INT16 len;

   len = PutIntParam( (INT32)TestForException(flag), string, 0 );
   len += PutAsciiParam( (BIT8 *)",", string + len );

   return len;
}

static INT16 DoDTDA( CMD_TYPE *p_cmd, BIT8  *p_resp_parm )
{
printf("call DoDTDA\n");
return 0;
/*
   INT16 parm_len;

   // Execute based upon input parameter 
   switch ( p_cmd->parms[p_cmd->parmv[1]] )
   {
      case '0':
         // There is no a-to-d loop back test 
         return( PutIntParam( (INT32)NOT_ALLOWED, p_resp_parm, 0 ) );

      case '1':
         // Report the a-to-d loop back test status
         return( PutIntParam( (INT32)GetDiagStatus(), p_resp_parm, 0 ) );

      case '2':
         // Report the a-to-d loop back test results 
         parm_len  = PutExceptionParam(ATTN1_TEST_FAILED, p_resp_parm);
         parm_len += PutExceptionParam(ATTN2_TEST_FAILED, p_resp_parm+parm_len);
         parm_len += PutExceptionParam(DAC1_TEST_FAILED, p_resp_parm+parm_len);
         parm_len += PutExceptionParam(DAC2_TEST_FAILED, p_resp_parm+parm_len);
         parm_len--;                     // strip off the final comma 
         FreeIODiagResource();
         return( parm_len );

      default:
         return( PutIntParam( (INT32)INVALID_PARAM, p_resp_parm, 0 ) );
   }
*/
}

/***********************************************************************/
/* END FUNCTION: DoDTDA                                                */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: DoDTCM                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Execute and get the results of a communications hardware   */
/*          test.                                                      */
/*                                                                     */
/* RETURNED VALUE: length of the response string                       */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTCM( CMD_TYPE *p_cmd, BIT8  *p_resp_parm )
{
printf("call DoDTCM\n");
return 0;
/*
   INT32   test_param;
   UI_ERR  error;

   if (p_cmd->parmv[0] == 0)
   {
      return  PutIntParam((INT32)MISSING_PARAM, p_resp_parm, 0);
   }

   error = DecToInt(&p_cmd->parms[p_cmd->parmv[1]],0,&test_param);

   // check for valid parameters 
   if( error != OK )
   {
      return  PutIntParam((INT32)INVALID_PARAM,p_resp_parm,0);
   }

   // Execute based upon test parameter 
   switch ( test_param )
   {
      case 0:
         // Host RS232 test 
         return  PutIntParam((INT32)Rs232SelfTest(HOST_PORT),p_resp_parm,0);

      case 1:
         // Host RS232 data loopback 
         return  PutIntParam(
            (INT32)Rs232DataLoopbackTest(HOST_PORT),p_resp_parm,0);

      case 2:
         // Host RS232 control loopback 
         return  PutIntParam(
            (INT32)Rs232ControlLoopbackTest(HOST_PORT),p_resp_parm,0);

      case 3:
         // OBC RS232 test 
         return  PutIntParam((INT32)Rs232SelfTest(LAN_PORT),p_resp_parm,0);

      case 4:
         // OBC RS232 data loopback 
         return  PutIntParam(
            (INT32)Rs232DataLoopbackTest(LAN_PORT),p_resp_parm,0);

      case 5:
         // OBC RS232 control loopback 
         return  PutIntParam(
            (INT32)Rs232ControlLoopbackTest(LAN_PORT),p_resp_parm,0);

      case 6:
         // Injector 1 RS232 test
         return  PutIntParam((INT32)Rs232SelfTest(INJECTOR1_PORT),p_resp_parm,0);

      case 7:
         // Injector 1 RS232 data loopback 
         return  PutIntParam(
            (INT32)Rs232DataLoopbackTest(INJECTOR1_PORT),p_resp_parm,0);

      case 8:
         // Injector 1 RS232 control loopback 
         return  PutIntParam(
            (INT32)Rs232ControlLoopbackTest(INJECTOR1_PORT),p_resp_parm,0);

      case 9:
         // Injector 2 RS232 test 
         return  PutIntParam((INT32)Rs232SelfTest(INJECTOR2_PORT),p_resp_parm,0);

      case 10:
         // Injector 2 RS232 data loopback 
         return  PutIntParam(
            (INT32)Rs232DataLoopbackTest(INJECTOR2_PORT),p_resp_parm,0);

      case 11:
         // Injector 2 RS232 control loopback 
         return  PutIntParam(
            (INT32)Rs232ControlLoopbackTest(INJECTOR2_PORT),p_resp_parm,0);

      case 12:
         // Tray RS232 test 
         return  PutIntParam((INT32)Rs232SelfTest(TRAY_PORT),p_resp_parm,0);

      case 13:
         // Tray RS232 data loopback 
         return  PutIntParam(
            (INT32)Rs232DataLoopbackTest(TRAY_PORT),p_resp_parm,0);

      case 14:
         // Tray RS232 control loopback 
         return  PutIntParam(
            (INT32)Rs232ControlLoopbackTest(TRAY_PORT),p_resp_parm,0);

      default:
         return  PutIntParam((INT32)INVALID_PARAM,p_resp_parm,0);
   }
*/
}

/***********************************************************************/
/* END FUNCTION: DoDTCM                                                */
/***********************************************************************/



/***********************************************************************/
/* FUNCTION: DoDTMX                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Execute the mux'ed adc tests.                              */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:  FreeDiagResource                                            */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTMX( CMD_TYPE *p_cmd, BIT8  *p_resp_parm )
{
printf("call DoDTMX\n");
return 0;
/*
   INT16       parm_len;
   INT16       i;
   U_INT16     *value;

   (void)p_resp_parm;

   parm_len = NO_RESPONSE;

   if ( p_cmd->parmv[0] < 1 )
   {
      // missing parameters
      CmdErrorHandler( p_cmd, 1, MISSING_PARAM );
      return parm_len;
   }

   // See which test to execute 
   switch ( p_cmd->parms[p_cmd->parmv[1]] )
   {
      case '0':
         // Execute the time response test 
         if ( StartMuxAdcTimeResponseTest( (BIT8)FROM_IO )  !=  TRUE )
         {
            CmdErrorHandler( p_cmd, 1, NOT_ALLOWED );
         }

         // Now wait for the results 
         while ( Mux_Adc_Time_Response_Test == TRUE )
         {
         }

         value = (U_INT16 *)&Diag_State.value[0];
         value++;
         parm_len = 0;
         for ( i=0; i<18; i++ )
         {
           parm_len += PutIntParam( *value, p_resp_parm+parm_len,0 );
           value++;
           parm_len += PutAsciiParam((BIT8 *)",", p_resp_parm+parm_len );
         }

         parm_len += PutIntParam( *value, p_resp_parm+parm_len,0 );

         // We have gotten the data, so free the diag resource for other tests
         FreeDiagResource();
         break;

   }

   return( parm_len );
*/
}

/***********************************************************************/
/* END FUNCTION: DoDTMX                                                */
/***********************************************************************/




/***********************************************************************/
/* FUNCTION: DoDTPS                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Execute the power supply diagnostic test.                  */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* FILE SCOPE STATICS USED:                                            */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTPS( BIT8  *p_resp_parm )
{
   INT16       parm_len;
printf("call DoDTPS\n");
/*
   parm_len = PutIntParam( Get5VMonitorInmV(), p_resp_parm,0 );
   parm_len += PutAsciiParam((BIT8 *)",",p_resp_parm+parm_len );

   parm_len += PutIntParam( Get15VposMonitorInmV(),p_resp_parm+parm_len,0 );
   parm_len += PutAsciiParam((BIT8 *)",",p_resp_parm+parm_len );

   parm_len += PutIntParam( Get15VnegMonitorInmV(),p_resp_parm+parm_len,0 );
   parm_len += PutAsciiParam((BIT8 *)",",p_resp_parm+parm_len );

   parm_len += PutIntParam( Get24VMonitorInmV(),p_resp_parm+parm_len,0 );
*/
   return( parm_len );

}

/***********************************************************************/
/* END FUNCTION: DoDTPS                                                */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: DoDTSK                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Set the next power-on sequence to skip the power-on test.  */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/* GLOBALS USED:  Skip_Power_On_Test                                   */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTSK( void )
{
   Skip_Power_On_Test = TRUE;

   return NO_RESPONSE;
}

/***********************************************************************/
/* END FUNCTION: DoDTSK                                                */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: DoDTRV                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Return the firmware version string.                        */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTRV( BIT8  *p_resp_parm )
{
printf("call DoDTRV\n");
return 0;
//   return TransferVersion( p_resp_parm );
}

/***********************************************************************/
/* END FUNCTION: DoDTRV                                                */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: DoDTRD                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Return the firmware date string.                           */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/
const char Build_ID[]="Fri 08 Sep 06 08:02"; /**/

static INT16 DoDTRD( BIT8  *p_resp_parm )
{
   U_INT16 len;

   len = 0;
   while ( Build_ID[len] )
   {
      *p_resp_parm++ = Build_ID[len++];
   }
   return len;
}

/***********************************************************************/
/* END FUNCTION: DoDTRD                                                */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: DoDTZP                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Report the wattage sense values for the thermal zones.     */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/* GLOBALS USED:   ZONE_CONFIG's                                       */
/*                                                                     */
/* CALLS:  DetermineInletZoneConfig or                                 */
/*         DetermineDetZoneConfig or                                   */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTZF( CMD_TYPE *p_cmd )
{
printf("call DoDTZF\n");
return 0;
/*
   pZONE_TABLE pzone;
   UI_ERR      error;
   INT32       value;
   INLET_TYPE  inlet_type;
   DET_TYPE    det_type;

   error = OK;

   if ( (error=DecToInt( &p_cmd->parms[p_cmd->parmv[1]], 0, &value )) != OK )
   {
      CmdErrorHandler( p_cmd, 1, error );
      return NO_RESPONSE;                                   // bug out 
   }

   switch (value)
   {
      case DETA_ZONE: det_type = DetectorA_Config.type;
                      DetermineDetZoneConfig(det_type,&Deta_Table);
                      break;

      case DETB_ZONE: det_type = DetectorB_Config.type;
                      DetermineDetZoneConfig(det_type,&Detb_Table);
                      break;

      case INJA_ZONE: pzone = &Inja_Table;
                      inlet_type = GetHonestInletType( pFRONT_INLET );
                      DetermineInletZoneConfig(inlet_type,pzone);
                      break;

      case INJB_ZONE: pzone = &Injb_Table;
                      inlet_type = GetHonestInletType( pBACK_INLET );
                      DetermineInletZoneConfig(inlet_type,pzone);
                      break;

      default:        CmdErrorHandler( p_cmd, 1, NOT_ALLOWED );
   }

   return NO_RESPONSE;                                       // no reply 
*/
}

/***********************************************************************/
/* END FUNCTION: DoDTZC                                                */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: DoDTZP                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Report the wattage sense values for the thermal zones.     */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/* GLOBALS USED:   ZONE_CONFIG's                                       */
/*                                                                     */
/* CALLS:  GetZoneWattageRange                                         */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTZP( BIT8 *string )
{
printf("call DoDTZP\n");
   INT16     len;
   ZONE_TYPE i;

   len = 0;
/*
   for ( i= DETA_ZONE; i<= AUXB_ZONE; i++ )
   {
      len += PutIntParam( (INT32)GetZoneWattageRange(i), string + len, 0 );
      len += PutAsciiParam( (BIT8 *)",", string + len );
   }
   len--;  // remove the trailing comma 
*/
   return len;
}

/***********************************************************************/
/* END FUNCTION: DoDTZP                                                */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: DoDTZG                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Set/Report the PID constants for a zone.                   */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/* GLOBALS USED:   ZONE_CONFIG's                                       */
/*                                                                     */
/* CALLS:  GetPropGain,GetIntegGain,GetDerivGain,CheckPassword         */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTZG( CMD_TYPE *p_cmd, BIT8 *string )
{
printf("call DoDTZG\n");
return 0;
/*
   INT16       len;
   pZONE_TABLE pzone;
   UI_ERR      error;
   INT32       value;

   len   = 0;
   pzone = pZone_Tune;

   // Check if the host wants to read the PID constants 
   if ( p_cmd->parms[0] == '?' )
   {
      len += PutIntParam( GetPropGain( pzone ), string + len, 0 );
      len += PutAsciiParam( (BIT8 *)",", string + len );
      len += PutIntParam( GetIntegGain( pzone ), string + len, 0 );
      len += PutAsciiParam( (BIT8 *)",", string + len );
      len += PutIntParam( GetDerivGain( pzone ), string + len, 0 );
      return len;
   }

   // Get the password 
   if ( CheckPassword( &p_cmd->parms[p_cmd->parmv[1]] ) == FALSE )
   {
      CmdErrorHandler( p_cmd, 0, NOT_ALLOWED );
      return NO_RESPONSE;                                       // Bug out/
   }

   // Set the PID parameters /

///////
// P //
///////
   error = OK;
   if ( (error=DecToInt( &p_cmd->parms[p_cmd->parmv[2]], 0, &value )) == OK )
   {
      error = SetPropGain( pZone_Tune, value );
   }
   if ( error != OK ) CmdErrorHandler( p_cmd, 1, error );

///////
// I //
///////
   error = OK;
   if ( (error=DecToInt( &p_cmd->parms[p_cmd->parmv[3]], 0, &value )) == OK )
   {
      error = SetIntegGain( pZone_Tune, value );
   }
   if ( error != OK ) CmdErrorHandler( p_cmd, 2, error );

///////
// D //
///////
   error = OK;
   if ( (error=DecToInt( &p_cmd->parms[p_cmd->parmv[4]], 0, &value )) == OK )
   {
      error = SetDerivGain( pZone_Tune, value );
   }
   if ( error != OK ) CmdErrorHandler( p_cmd, 3, error );

   return NO_RESPONSE;
*/
}

/***********************************************************************/
/* END FUNCTION: DoDTZG                                                */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: DoDTPK                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Peek or poke memory.                                       */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/* CALLS:  Sets/Gets for PlotMemLoc                                    */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTPK(CMD_TYPE *p_cmd, BIT8 *p_resp_parm )
{
printf("call DoDTPK\n");
return 0;
/*
   UI_ERR    error;
   U_INT16   length,type;
   U_INT32   address;
   INT32     value;
   DATA_SIZE size;

   length = 0;

   if ( p_cmd->parmv[0] < 3 )
   {
      return PutIntParam( (INT32)MISSING_PARAM,p_resp_parm,0 );
   }

   // Get read/write test type 
   type = p_cmd->parms[0] - '0';

   // Get the address   /
   if ((error = HexToInt(&p_cmd->parms[p_cmd->parmv[2]], &address )) == OK )
   {
      // Get the data size /
      if ((error = DecToInt(&p_cmd->parms[p_cmd->parmv[3]], 0, &value)) == OK )
      {
         if ( (error = GetDataSize( value, &size )) == OK )
         {
            if ( type == 0 )
            {
               //read address location/
               value  = 0;
               if ( (error = CheckSignalMemLoc((U_INT32 *)address,size) ) == OK )
               {
                  value = PeekMemory( (U_INT32 *)address, size );
               }
               length = PutIntParam( (INT32)error, p_resp_parm, 0 );
               length += PutAsciiParam((BIT8 *)",",p_resp_parm+length );

               // Exit with result code and value/
               return (length + PutHexParam( value, p_resp_parm+length ) );
            }
            else
            {
               if ( type == 1 )
               {
                  if ((error = HexToInt(&p_cmd->parms[p_cmd->parmv[4]], (U_INT32 *)&value )) == OK )
                  {
                     if ( (error = CheckWritableMemLoc( (U_INT32 *)address,
                                            size ) ) == OK )
                     {
                        PokeMemory( (U_INT32 *)address, size, value );
                     }
                  }
               }
               else
               {
                  error  = INVALID_PARAM;
               }
            }
         }
      }
   }

   // Exit with result code /
   return PutIntParam( (INT32)error, p_resp_parm, 0 );
*/
}

/***********************************************************************/
/* END FUNCTION: DoDTPK                                                */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: UIntToDec                                                 */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Convert 32 bit unsigned int to ASCII decimal               */
/*                                                                     */
/* RETURNED VALUE: length of string                                    */
/*                                                                     */
/* CALLS:  UIntToDec                                                   */
/*                                                                     */
/* LIMITATIONS:  THIS FUNCTION IS NOT RE-ENTRANT!!!                    */
/*                                                                     */
/* WARNINGS:                                                           */
/*                                                                     */
/***********************************************************************/

U_INT16 UIntToDec(U_INT32 dec,BIT8 *str)
{
   U_INT16 len;

   len = 0;

   if( dec >= 10)                  /* first recurse for each digit */
   {
      len = UIntToDec(dec/10, str);
      dec %= 10;                   /* process each digit by modulo */
   }
   *(str+len) = '0' + (char)dec;     /* output as character */
   return  (len+1);
}

/***********************************************************************/
/* END FUNCTION: UIntToDec                                             */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: IntToDecFilled                                            */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Convert 32 bit signed int to ASCII decimal                 */
/*                                                                     */
/* RETURNED VALUE: length of string                                    */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/* WARNINGS:                                                           */
/*                                                                     */
/***********************************************************************/

U_INT16 IntToDecFilled( INT32 dec, BIT8 *str, INT16 size )
{
   INT32   value;
   INT16   i;

   for (i=size-1; i>=0; i--)
   {
      value = dec % 10;                 /* get the last digit */
      dec /= 10;                        /* process each digit by division */
      *(str+i) = '0' + (char)value;     /* output as character */
   }
   return size;

}
/***********************************************************************/
/* END FUNCTION: IntToDecFilled                                        */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: DoDTIO                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Set/Clear/Report Bits in I/O Registers                     */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

typedef struct
{
 //  volatile BIT8  *RegImage;
 //  volatile BIT8  *RegAddr;
   BIT8  *RegImage;
   BIT8  *RegAddr;
            BIT8   ReadOnly;
} REG_INFO_TYPE;
const REG_INFO_TYPE REG_INFO [] =
{
/* 0*/   { (BIT8 *)&APG_BUS_IN,        (BIT8 *)&APG_BUS_IN,        0xff },
/* 1*/   { (BIT8 *)&BCD_IN,            (BIT8 *)&BCD_IN,            0xff },
#if 0
/* 2*/   { (BIT8 *)&DETECTOR_IN,       (BIT8 *)&DETECTOR_IN,       0xff },
/* 3*/   { (BIT8 *)&DISCRETE_IN2,      (BIT8 *)&DISCRETE_IN2,      0xff },
#endif
/* 2*/   { (BIT8 *)&KEYBOARD,          (BIT8 *)&KEYBOARD,          0xff },
/* 3*/   { (BIT8 *)&ZONES_CONFIG1,     (BIT8 *)&ZONES_CONFIG1,     0xff },
/* 4*/   { (BIT8 *)&ZONES_CONFIG2,     (BIT8 *)&ZONES_CONFIG2,     0xff },
#if 0
/* 6*/   { (BIT8 *)&DiscreteOut1_Image,(BIT8 *)&DETECTOR_OUT1,     0x00 },
/* 6*/   { (BIT8 *)&DiscreteOut2_Image,(BIT8 *)&DETECTOR_OUT2,     0x00 },
#endif
/* 5*/   { (BIT8 *)&Reset1_Image,      (BIT8 *)&RESET_LATCH1,      0x1f },
/* 6*/   { (BIT8 *)&Zones_Image,       (BIT8 *)&ZONES_OUT1,        0xdc },
/* 7*/   { (BIT8 *)&Zones_Image,       (BIT8 *)&ZONES_OUT2,        0xdc },
#if 0
/*11*/   { (BIT8 *)&Apg_Out_Image,     (BIT8 *)&APG_BUS_OUT,       0x00 },
#endif
};

static INT16 DoDTIO(CMD_TYPE *p_cmd, BIT8 *p_resp_parm )
{
            INT32    regnum;
            INT32    bitnum;
            BIT8     bitmask;
            BIT8     setreset;
            INT32    setval;
   volatile BIT8    *pImage;
   volatile BIT8    *pReg;
            BIT8     allowmask;
            UI_ERR   error;

   /* Make sure both the register and the bit are specified */
   if ( p_cmd->parmv[0] < 2 )
   {
      CmdErrorHandler( p_cmd, 0, MISSING_PARAM );
      return NO_RESPONSE;
   }

   /* Get the register number */
   error = DecToInt( &p_cmd->parms[p_cmd->parmv[1]], 0, &regnum );
   if (error != OK)
   {
      CmdErrorHandler( p_cmd, 1, error );
      return NO_RESPONSE;
   }

   /* Get the bit number */
   error = DecToInt( &p_cmd->parms[p_cmd->parmv[2]], 0, &bitnum );
   if (error != OK)
   {
      CmdErrorHandler( p_cmd, 2, error );
      return NO_RESPONSE;
   }

   /* See if this is Set, Reset or Test */
   if (p_cmd->parmv[0] < 3)
   {
      /* Test only */
      setreset = FALSE;
      setval   = 0;       /* for lint only, not used */
   }
   else
   {
      /* Set or Reset */
      setreset = TRUE;

      /* Get the state to set it to */
      error = DecToInt( &p_cmd->parms[p_cmd->parmv[3]], 0, &setval );
      if (error != OK)
      {
         CmdErrorHandler( p_cmd, 3, error );
         return NO_RESPONSE;
      }
   }

   /* Now range check all parameters */
   if ( (regnum < 0) || (regnum > 7) )
   {
      CmdErrorHandler( p_cmd, 1, INVALID_PARAM );
      return NO_RESPONSE;
   }
   if ( (bitnum < 0) || (bitnum > 7) )
   {
      CmdErrorHandler( p_cmd, 2, INVALID_PARAM );
      return NO_RESPONSE;
   }
   if ( setreset && ((setval < 0) || (setval > 1)) )
   {
      CmdErrorHandler( p_cmd, 3, INVALID_PARAM );
      return NO_RESPONSE;
   }

   /* Get the register info from the table */
   pImage    = REG_INFO[regnum].RegImage;
   pReg      = REG_INFO[regnum].RegAddr;
   allowmask = REG_INFO[regnum].ReadOnly;

   bitmask = 1 << bitnum;

   if (setreset)
   {
      /* Handle Bit Set/Reset */
      /* See if allowed for this register */
      if (allowmask & bitmask)
      {
         /* NOT allowed */
         CmdErrorHandler( p_cmd, 0, NOT_ALLOWED );
         return NO_RESPONSE;
      }
      /* Set/Reset OK, see which one */
      if (setval)
      {
         *pImage |= bitmask;
      }
      else
      {
         *pImage &= ~bitmask;
      }
      *pReg = *pImage;
      return NO_RESPONSE;
   }
   else
   {
      /* Handle Bit Test */
      if (*pImage & bitmask)
      {
         return PutAsciiParam( (BIT8 *)"1", p_resp_parm );
      }
      else
      {
         return PutAsciiParam( (BIT8 *)"0", p_resp_parm );
      }
   }
}

/***********************************************************************/
/* END FUNCTION: DoDTIO                                                */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: DoDTMD                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Set/Report manufacturing date                              */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/* GLOBALS USED:  When_Manufactured                                    */
/*                                                                     */
/* CALLS:  Sets for year,month,day and hour,minute,second              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTMD(CMD_TYPE *p_cmd, BIT8 *p_resp_parm )
{
  printf("call DoDTMD\n");
  return NO_RESPONSE;

//    INT32    value;
//    UI_ERR   error;
//    INT16    len;
// 
//    /* Check if the host wants to read the datetime */
//    if ( p_cmd->parms[0] == '?' )
//    {
//       len = IntToDecFilled(  GetWhenManufacturedDate(), p_resp_parm, 6);
//       len += PutAsciiParam( (BIT8 *)",",     p_resp_parm+len );
//       value =When_Manufactured.hour*10000+When_Manufactured.minute*100+When_Manufactured.second;
//       len += IntToDecFilled( value, p_resp_parm+len, 6);
//       return len;
//    }
// 
//    /* Get the password */
//    if ( CheckPassword( &p_cmd->parms[p_cmd->parmv[1]] ) == FALSE )
//    {
//       CmdErrorHandler( p_cmd, 0, NOT_ALLOWED );
//       return NO_RESPONSE;                                       /* Bug out */
//    }
// 
//    /* Get the day, month, and year parameters */
// 
//    error = OK;
//    if ( (error=DecToInt( &p_cmd->parms[p_cmd->parmv[2]], 0, &value )) == OK )
//    {
//       /* Set the date, given the input format of day,month,year */
//       error = SetDayMonthYear( &When_Manufactured, value );
//    }
// 
//    if ( error != OK )
//    {
//       CmdErrorHandler( p_cmd, 1, error );     /* invalid date */
//       error = OK;
//    }
// 
//    /* Get the hour, minute, and second parameters */
// 
//    if ( (error=DecToInt( &p_cmd->parms[p_cmd->parmv[3]], 0, &value )) == OK )
//    {
//       error = SetHourMinuteSecond( &When_Manufactured, value );
//    }
// 
//    /* See if the hour,minute,second were accepted */
//    if ( error != OK )
//    {
//       CmdErrorHandler( p_cmd, 2, error );
//    }
// 
//    return NO_RESPONSE;
}

/***********************************************************************/
/* END FUNCTION: DoDTMD                                                */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: DoDTSN                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Set/Report GC serial number                                */
/*                                                                     */
/* RETURNED VALUE: INT16 response length                               */
/*                                                                     */
/* GLOBALS USED:  Serial_No                                            */
/*                                                                     */
/* CALLS:  TransferVersion, CheckPassword, SetSerialNumber             */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTSN(CMD_TYPE *p_cmd, BIT8 *p_resp_parm )
{
  printf("call DoDTSN\n");
   UI_ERR   error;
   INT16    len;

//    /* Check if the host wants to read the serial number */
//    if ( p_cmd->parms[0] == '?' )
//    {
//       len = GetSerialNumber( p_resp_parm );
//       return len;
//    }
// 
//    /* Get the password */
//    if ( CheckPassword( &p_cmd->parms[p_cmd->parmv[1]] ) == FALSE )
//    {
//       CmdErrorHandler( p_cmd, 0, NOT_ALLOWED );
//       return NO_RESPONSE;                                       /* Bug out */
//    }
// 
//    /* Set the serial number */
//    error = SetSerialNumber( &p_cmd->parms[p_cmd->parmv[2]] );
// 
//    if ( error != OK )
//    {
//       CmdErrorHandler( p_cmd, 1, error );     /* invalid serial number */
//    }

   return NO_RESPONSE;
}

/***********************************************************************/
/* END FUNCTION: DoDTSN                                                */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: DoDTSC                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Read Als_Power_Mode.  Set ON (1) or OFF (0).               */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/* CALLS:  none                                                        */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTSC( CMD_TYPE  *p_cmd, BIT8 *p_resp_parm )
{
  printf("call DoDTSC\n");
//    U_INT8  param;
// 
//    param = p_cmd->parms[0];
// 
//    if ( param == '?' )
//    {
//       return UIntToDec( (U_INT32) Als_Power_Mode, p_resp_parm );
//    }
// 
//    if ( param == '0' )
//    {
//       Als_Power_Mode = KEEP_OFF;
//    }
//    else
//    {
//       if ( param == '1' )
//       {
//          Als_Power_Mode = POWER_OFF;  /* re-enables state machine */
//       }
//       else
//       {
//          CmdErrorHandler(p_cmd,1,INVALID_PARAM);
//       }
//    }

   return NO_RESPONSE;
}

/***********************************************************************/
/* END FUNCTION: DoDTSC                                                */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: DoDTSP                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Reprot Sampler Power INFO                                  */
/*                                                                     */
/* RETURNED VALUE: INT16 response length                               */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTSP(CMD_TYPE *p_cmd, BIT8 *p_resp_parm )
{
  printf("call DoDTSP\n");
   INT16    len;
// 
//    (void) p_cmd;
// 
//    len = PutIntParam( (INT32) Als_Power_Mode, p_resp_parm, 0 );
//    len += PutAsciiParam( (BIT8 *)",", p_resp_parm+len );
//    len += PutIntParam( Als_Power_Max, p_resp_parm+len, 0);
//    Als_Power_Max = 0;   /* clear variable after reading */
   return len;
}

/***********************************************************************/
/* END FUNCTION: DoDTSP                                                */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: DoDTST                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Report instrument statistics: uptime and # of start runs.  */
/*                                                                     */
/* RETURNED VALUE: INT16 response length                               */
/*                                                                     */
/* GLOBALS USED:  Uptime_Clock, Run_Count                              */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTST( CMD_TYPE *p_cmd,  BIT8 *p_resp_parm )
{
  printf("call DoDTST\n");
//   INT16  length;
//    INT32  value;
//    INT32  old_value;
//    U_INT32 old_clock;
//    UI_ERR error;
//    INT16  i;
// 
//    length = 0;
// 
//    /* Check if the host wants to read the status */
//    if ( p_cmd->parms[0] == '?' )
//    {
//       length = PutIntParam( Uptime_Clock, p_resp_parm, 0 );
//       length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
//       length += PutIntParam( Run_Count, p_resp_parm+length, 0 );
//       for (i = 0; i < 5; i++)
//       {
//          length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
//          length += PutIntParam( (Uptime_Clock + uptime[i]), p_resp_parm+length, 0 );
//          length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
//          length += PutIntParam( (Run_Count + runs[i]), p_resp_parm+length, 0 );
//       }
//       return length;
//    }
// 
//    /* Check if the host wants to set one or more values */
//    if (p_cmd->parmv[0] < 2)
//    {
//       CmdErrorHandler( p_cmd, 0, MISSING_PARAM );
//       return NO_RESPONSE;                                       /* Bug out */
//    }
// 
//    /* Get the password */
//    if ( CheckPassword( &p_cmd->parms[p_cmd->parmv[1]] ) == FALSE )
//    {
//       CmdErrorHandler( p_cmd, 0, NOT_ALLOWED );
//       return NO_RESPONSE;                                       /* Bug out */
//    }
// 
//    /* Set the master uptime and start run count */
// 
//    /**********/
//    /* uptime */
//    /**********/
// 
//    if (p_cmd->parms[p_cmd->parmv[2]])
//    {
//       error = OK;
//       old_clock = Uptime_Clock;
//       if ( (error=DecToInt( &p_cmd->parms[p_cmd->parmv[2]], 0, &value )) == OK )
//       {
//          error = SetUptime( value );
//       }
//       if ( error != OK ) CmdErrorHandler( p_cmd, 1, error );
//       /* decreasing the uptime clock could screw up the exception log */
//       if (Uptime_Clock < old_clock)
//       {
//          (void) DeleteExceptionLog();
//       }
//       for (i = 0; i < 5; i++)
//       {
//          uptime[i] = uptime[i] + old_clock - Uptime_Clock;
//       }
//    }
// 
//    /* Check if host wants to set the master run count */
//    if (p_cmd->parmv[0] < 3)
//    {
//       return NO_RESPONSE;
//    }
// 
//    /*************/
//    /* run count */
//    /*************/
// 
//    if (p_cmd->parms[p_cmd->parmv[2]])
//    {
//       error = OK;
//       old_value = Run_Count;
//       if ( (error=DecToInt( &p_cmd->parms[p_cmd->parmv[3]], 0, &value )) == OK )
//       {
//          error = SetRunCount( value );
//       }
//       if ( error != OK ) CmdErrorHandler( p_cmd, 2, error );
//       for (i = 0; i < 5; i++)
//       {
//          runs[i] = runs[i] + old_value - Run_Count;
//       }
//    }
// 
//    /* Handle the slave counters, both uptime and run count */
//    for (i = 4; i <= p_cmd->parmv[0]; i++)
//    {
//       if (p_cmd->parms[p_cmd->parmv[i]])
//       {
//          error = OK;
//          if ( (error=DecToInt( &p_cmd->parms[p_cmd->parmv[i]], 0, &value )) == OK )
//          {
//             if (value >= 0)
//                if (i & 1)
//                {
//                   runs[(i-5)/2] = value - Run_Count;
//                }
//                else
//                {
//                   uptime[(i-4)/2] = value - Uptime_Clock;
//                }
//             else
//             {
//                error = INVALID_PARAM;
//             }
//          }
//          if ( error != OK ) CmdErrorHandler( p_cmd, i, error );
//       }
//    }

   return NO_RESPONSE;                                       /* All done */
}

/***********************************************************************/
/* END FUNCTION: DoDTST                                                */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: DoDTXP                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Execute a Pnematics PCB Command                            */
/*                                                                     */
/* RETURNED VALUE: the response from the pneumatics dsp                */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* CALLS:  the pneumatics generic command executor                     */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTXP(CMD_TYPE *p_cmd, BIT8 *p_resp_parm )
{
   INT32    i;
   UI_ERR   error;
   U_INT16  length;
   U_INT16  cmd[16];
   U_INT16  rsp[16];

   /* See if we have a pneumatics board installed */
   if ( IamAdirtball() )
   {
      CmdErrorHandler( p_cmd, 0, NOT_INSTALLED );
      return NO_RESPONSE;
   }

   for ( i = 0 ; i < 16 ; i++ )
   {
          cmd[i] = 0;
          rsp[i] = 0;
   }

   error = OK;

   for ( i = 1 ; i <= MIN(p_cmd->parmv[0],16) ; i++ )
   {
       if ( (error = GetAWordParam( p_cmd, i, &cmd[i-1] )) != OK ) break;
   }


   if ( error == OK )
   {
       (void) SendPcbCmd( cmd, rsp );
   }
   else
   {
       CmdErrorHandler( p_cmd, i, error );
       return NO_RESPONSE;
   }

   length = 0;

   for ( i = 0 ; i < 16 ; i++ )
   {
       length += PutIntParam( rsp[i],            p_resp_parm+length, 0 );
       length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   }

   return length;
}

/***********************************************************************/
/* END FUNCTION: DoDTXP                                                */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: DoDTXR                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Performs a cross reference address lookup                  */
/*                                                                     */
/* RETURNED VALUE: The symbol name and address                         */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTXR(CMD_TYPE *p_cmd, BIT8 *p_resp_parm )
{
   const BIT8      *pXREF;
         BIT8      *pNAME;
         BIT8       name0;
         INT32      i;
         INT32      maxindex;
         UI_ERR     error;
         U_INT16    length;

   maxindex = ( (sizeof( xref ) / sizeof( XREF_TYPE )) - 1 );
   i = 0;
   if (p_cmd->parmv[0] > 0)
   {
      error = DecToInt ( &p_cmd->parms[p_cmd->parmv[1]], 0, &i );
      if ( error != OK )
      {
         i = -1;
      }
      if ( (i < 0 ) || (i > maxindex) )
      {
         i = maxindex;
      }
      if (i == 0 )
      {
         name0 = p_cmd->parms[p_cmd->parmv[1]];
         for ( i = 1; i < maxindex; i++ )
         {
            if ( xref[i].name[0] == name0 )
            {
               pXREF = xref[i].name;
               pNAME = &(p_cmd->parms[p_cmd->parmv[1]]);
               while ( (*pXREF) && (*pXREF == *pNAME) )
               {
                  pXREF++;
                  pNAME++;
               }
               if ( *pXREF == 0 )
               {
                  break;
               }
            }
         }
      }
   }

   length = 0;
   length  = PutHexParam( (U_INT32)xref[i].address, p_resp_parm+length );
   length += PutAsciiParam((BIT8 *)",", p_resp_parm+length);
   length += PutAsciiParam((BIT8 *)"\"", p_resp_parm+length);
   length += PutAsciiParam((BIT8 *)xref[i].name, p_resp_parm+length);
   length += PutAsciiParam((BIT8 *)"\"", p_resp_parm+length);


   return length;
}

/***********************************************************************/
/* END FUNCTION: DoDTXR                                                */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: DoDTMI                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Report a pneumatics module's build information             */
/*                                                                     */
/* RETURNED VALUE: serial number, type, version, build date            */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* CALLS:  the pneumatics access routines                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTMI(CMD_TYPE *p_cmd, BIT8 *p_resp_parm )
{
   INT32    i;
   UI_ERR   error;
   U_INT16  length;
   U_INT16  year;
   U_INT8   month,day;

   length = 0;


   error = OK;

   if ( (error=DecToInt( &p_cmd->parms[p_cmd->parmv[1]], 0, &i )) != OK )
   {
      CmdErrorHandler( p_cmd, 1, error );     /* invalid param */
      return NO_RESPONSE;
   }
   else
   {
      if ( ( i < 0 ) || ( i > PNEU_AUX_MODULE_NUMBER ) )
      {
         CmdErrorHandler( p_cmd, 1, INVALID_PARAM );     /* invalid param */
         return NO_RESPONSE;
      }

      length += UIntToDec( GetModuleSerialNumber(i), p_resp_parm+length );
      length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );

      length += PutHexParam( GetModuleType(i), p_resp_parm+length );

      length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );

      length += PutHexParam( GetModuleVersion(i), p_resp_parm+length );

      length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );

      GetModuleBuildDateParts( (U_INT8)i, &year, &month, &day);

      length += PutIntParam( day, p_resp_parm+length, 0 );
      length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
      length += PutIntParam( month, p_resp_parm+length, 0 );
      length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
      length += PutIntParam( year, p_resp_parm+length, 0 );

      length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
      length += PutIntParam( GetTestStationId( i ), p_resp_parm+length, 0 );
   }
   return length;
}

/***********************************************************************/
/* END FUNCTION: DoDTMI                                                */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: DoDTPR                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Return the actual setpoints and actuals for the pneumatics */
/*          with the NTP correction applied.                           */
/*                                                                     */
/* RETURNED VALUE: pressure and flow values                            */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

/* REVISIT: move to pi_util.c */

INT32 GetRealSetpoint( U_INT16 channel )
{
   switch( channel )
   {
   case 0:                      /* <FIPs>  */
        return ConvertPresIuToDcu( Frnt_Inlet_Tcomp_Tables.Pres_Sensor.Pres_Setpt,
                                   Inst_Status.Pneu.Frnt_Inlet.Dcm2_Per_IU );

   case 1:                      /* <FIFs>  */
        return ConvertFlowIuToDcu( Frnt_Inlet_Tcomp_Tables.Flow_Sensor.Flow_Setpt,
                                   Inst_Status.Pneu.Frnt_Inlet.IU_Scaling );

   case 2:                      /* <BIPs>  */
        return ConvertPresIuToDcu( Back_Inlet_Tcomp_Tables.Pres_Sensor.Pres_Setpt,
                                   Inst_Status.Pneu.Back_Inlet.Dcm2_Per_IU );

   case 3:                      /* <BIFs>  */
        return ConvertFlowIuToDcu( Back_Inlet_Tcomp_Tables.Flow_Sensor.Flow_Setpt,
                                   Inst_Status.Pneu.Frnt_Inlet.IU_Scaling );

   case 4:                      /* <FD1Ps> */
        return ConvertPresIuToDcu( Frnt_Det_Tcomp_Tables.Fuel.Pres_Sensor.Pres_Setpt,
                                   Inst_Status.Pneu.Frnt_Detector.Gas_1.Dcm2_Per_IU );

   case 5:                      /* <FD2Ps> */
        return ConvertPresIuToDcu( Frnt_Det_Tcomp_Tables.Util.Pres_Sensor.Pres_Setpt,
                                   Inst_Status.Pneu.Frnt_Detector.Gas_2.Dcm2_Per_IU );

   case 6:                      /* <FD3Ps> */
        return ConvertPresIuToDcu( Frnt_Det_Tcomp_Tables.Mug.Pres_Sensor.Pres_Setpt,
                                   Inst_Status.Pneu.Frnt_Detector.Gas_3.Dcm2_Per_IU );

   case 7:                      /* <BD1Ps> */
        return ConvertPresIuToDcu( Back_Det_Tcomp_Tables.Fuel.Pres_Sensor.Pres_Setpt,
                                   Inst_Status.Pneu.Back_Detector.Gas_1.Dcm2_Per_IU );

   case 8:                      /* <BD2Ps> */
        return ConvertPresIuToDcu( Back_Det_Tcomp_Tables.Util.Pres_Sensor.Pres_Setpt,
                                   Inst_Status.Pneu.Back_Detector.Gas_2.Dcm2_Per_IU );

   case 9:                      /* <BD3Ps> */
        return ConvertPresIuToDcu( Back_Det_Tcomp_Tables.Mug.Pres_Sensor.Pres_Setpt,
                                   Inst_Status.Pneu.Back_Detector.Gas_3.Dcm2_Per_IU );

   case 10:                     /* <AX3Ps> */
        return ConvertPresIuToDcu( Pneu_Aux_Tcomp_Tables.Aux_3.Pres_Setpt,
                                   Inst_Status.Pneu.Aux.Aux_3.Dcm2_Per_IU );

   case 11:                     /* <AX4Ps> */
        return ConvertPresIuToDcu( Pneu_Aux_Tcomp_Tables.Aux_4.Pres_Setpt,
                                   Inst_Status.Pneu.Aux.Aux_4.Dcm2_Per_IU );

   case 12:                     /* <AX5Ps> */
        return ConvertPresIuToDcu( Pneu_Aux_Tcomp_Tables.Aux_5.Pres_Setpt,
                                   Inst_Status.Pneu.Aux.Aux_5.Dcm2_Per_IU );
   }
   return 0;
}

INT32 GetRealActual( U_INT16 channel )
{
   switch( channel )
   {
   case 0:                      /* <FIPs>  */
        return ConvertPresIuToDcu( Frnt_Inlet_Tcomp_Tables.Pres_Sensor.Pres_Actual,
                                   Inst_Status.Pneu.Frnt_Inlet.Dcm2_Per_IU );

   case 1:                      /* <FIFs>  */
        return ConvertFlowIuToDcu( Frnt_Inlet_Tcomp_Tables.Flow_Sensor.Flow_Actual,
                                   Inst_Status.Pneu.Frnt_Inlet.IU_Scaling );

   case 2:                      /* <BIPs>  */
        return ConvertPresIuToDcu( Back_Inlet_Tcomp_Tables.Pres_Sensor.Pres_Actual,
                                   Inst_Status.Pneu.Back_Inlet.Dcm2_Per_IU );

   case 3:                      /* <BIFs>  */
        return ConvertFlowIuToDcu( Back_Inlet_Tcomp_Tables.Flow_Sensor.Flow_Actual,
                                   Inst_Status.Pneu.Frnt_Inlet.IU_Scaling );

   case 4:                      /* <FD1Ps> */
        return ConvertPresIuToDcu( Frnt_Det_Tcomp_Tables.Fuel.Pres_Sensor.Pres_Actual,
                                   Inst_Status.Pneu.Frnt_Detector.Gas_1.Dcm2_Per_IU );

   case 5:                      /* <FD2Ps> */
        return ConvertPresIuToDcu( Frnt_Det_Tcomp_Tables.Util.Pres_Sensor.Pres_Actual,
                                   Inst_Status.Pneu.Frnt_Detector.Gas_2.Dcm2_Per_IU );

   case 6:                      /* <FD3Ps> */
        return ConvertPresIuToDcu( Frnt_Det_Tcomp_Tables.Mug.Pres_Sensor.Pres_Actual,
                                   Inst_Status.Pneu.Frnt_Detector.Gas_3.Dcm2_Per_IU );

   case 7:                      /* <BD1Ps> */
        return ConvertPresIuToDcu( Back_Det_Tcomp_Tables.Fuel.Pres_Sensor.Pres_Actual,
                                   Inst_Status.Pneu.Back_Detector.Gas_1.Dcm2_Per_IU );

   case 8:                      /* <BD2Ps> */
        return ConvertPresIuToDcu( Back_Det_Tcomp_Tables.Util.Pres_Sensor.Pres_Actual,
                                   Inst_Status.Pneu.Back_Detector.Gas_2.Dcm2_Per_IU );

   case 9:                      /* <BD3Ps> */
        return ConvertPresIuToDcu( Back_Det_Tcomp_Tables.Mug.Pres_Sensor.Pres_Actual,
                                   Inst_Status.Pneu.Back_Detector.Gas_3.Dcm2_Per_IU );

   case 10:                     /* <AX3Ps> */
        return ConvertPresIuToDcu( Pneu_Aux_Tcomp_Tables.Aux_3.Pres_Actual,
                                   Inst_Status.Pneu.Aux.Aux_3.Dcm2_Per_IU );

   case 11:                     /* <AX4Ps> */
        return ConvertPresIuToDcu( Pneu_Aux_Tcomp_Tables.Aux_4.Pres_Actual,
                                   Inst_Status.Pneu.Aux.Aux_4.Dcm2_Per_IU );

   case 12:                     /* <AX5Ps> */
        return ConvertPresIuToDcu( Pneu_Aux_Tcomp_Tables.Aux_5.Pres_Actual,
                                   Inst_Status.Pneu.Aux.Aux_5.Dcm2_Per_IU );
   }
   return 0;
}

static INT16 DoDTPR( BIT8 *p_resp_parm )
{
   INT32    i;
   U_INT16  length;

   length = 0;

   for ( i = 0 ; i <= 12 ; i++ )
   {
       length += PutIntParam( GetRealSetpoint(i), p_resp_parm+length, 0 );
       length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   }

   for ( i = 0 ; i < 12 ; i++ )
   {
       length += PutIntParam( GetRealActual(i), p_resp_parm+length, 0 );
       length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   }
   length += PutIntParam( GetRealActual( i ), p_resp_parm+length, 0 );

   return length;
}

/***********************************************************************/
/* END FUNCTION: DoDTPR                                                */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: DoDTPV                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Return the voltages of the pneumatics sensors              */
/*                                                                     */
/* RETURNED VALUE: the filtered, average, offset corrected voltages    */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTPV( BIT8 *p_resp_parm )
{
   INT32    i;
   U_INT16  length;

   length = 0;

   for ( i = 0 ; i < NUM_PNEU_CHANNELS-1 ; i++ )
   {
       length += PutIntParam( GetChannelAvgVoltage( i ), p_resp_parm+length, 0 );
       length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   }
   length += PutIntParam( GetChannelAvgVoltage( i ), p_resp_parm+length, 0 );

   return length;
}

/***********************************************************************/
/* END FUNCTION: DoDTPV                                                */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: DoDTPN                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Return the noise of the pneumatics sensor adc readings     */
/*                                                                     */
/* RETURNED VALUE: the noise of the adc readings from the pneumatics   */
/*                 DSP                                                 */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTPN( BIT8 *p_resp_parm )
{
   INT32    i;
   U_INT16  length;

   length = 0;

   for ( i = 0 ; i < NUM_PNEU_CHANNELS-1 ; i++ )
   {
       length += PutIntParam( GetChannelNoise( i ), p_resp_parm+length, 0 );
       length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   }
   length += PutIntParam( GetChannelNoise( i ), p_resp_parm+length, 0 );

   return length;
}

/***********************************************************************/
/* END FUNCTION: DoDTPN                                                */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: DoDTMT                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Return the voltages of the pneumatics module temp sensors  */
/*                                                                     */
/* RETURNED VALUE: the filtered, average, offset corrected voltages    */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTMT( BIT8 *p_resp_parm )
{
   INT32    i;
   U_INT16  length;

   length = 0;

   for ( i = 0 ; i < 5 ; i++ )
   {
       length += PutIntParam( GetModuleThermistorVoltage( i ), p_resp_parm+length, 0 );
       length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   }


   for ( i = 0 ; i < 4 ; i++ )
   {
       length += PutIntParam( (U_INT32)GetModuleTemp( i )*100/64, p_resp_parm+length, 2 );
       length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   }
   length += PutIntParam( (U_INT32)GetModuleTemp( i )*100/64, p_resp_parm+length, 2 );


   return length;
}

/***********************************************************************/
/* END FUNCTION: DoDTMT                                                */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: DoDTEN                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Return the noise of the pneumatics board environment adc   */
/*          readings.                                                  */
/*                                                                     */
/* RETURNED VALUE: peak-to-peak noise in adc counts                    */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTEN( BIT8 *p_resp_parm )
{
   INT32    i;
   U_INT16  length;

   length = 0;

   for ( i = 0 ; i < NUM_ENV_CHANNELS-1 ; i++ )
   {
       length += PutIntParam( GetEnvNoise( i ), p_resp_parm+length, 0 );
       length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   }
   length += PutIntParam( GetEnvNoise( i ), p_resp_parm+length, 0 );

   return length;
}

/***********************************************************************/
/* END FUNCTION: DoDTEN                                                */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: DoDTEV                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Return the average, offset corrected environmental volts   */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTEV( BIT8 *p_resp_parm )
{
printf("call DoDTEV\n");
   INT32    i;
   U_INT16  length;

   length = 0;

   for ( i = 0 ; i < NUM_ENV_CHANNELS-1 ; i++ )
   {
       length += PutIntParam( GetEnvAvgVoltage( i ), p_resp_parm+length, 0 );
       length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   }
   length += PutIntParam( GetEnvAvgVoltage( i ), p_resp_parm+length, 0 );

   return length;
}

/***********************************************************************/
/* END FUNCTION: DoDTEV                                                */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: DoDTE1                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Return the latest raw thermal environment readings.        */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTE1( BIT8 *p_resp_parm )
{
   U_INT16  length;

   length = 0;

   length += PutIntParam( (INT32)(U_INT16)Mux_Adc_Offset2_Rdg.value, p_resp_parm+length, 0 );
   length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   length += PutIntParam( (INT32)(U_INT16)Mux_Adc_Offset_Rdg.value, p_resp_parm+length, 0 );
   length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   length += PutIntParam( (INT32)(U_INT16)Line_Sense.rdg.value, p_resp_parm+length, 0 );
   length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   length += PutIntParam( (INT32)(U_INT16)Det_Diaga_Rdg, p_resp_parm+length, 0 );
   length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   length += PutIntParam( (INT32)(U_INT16)Det_Diagb_Rdg, p_resp_parm+length, 0 );
   length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   length += PutIntParam( (INT32)(U_INT16)DAC1_Rdg, p_resp_parm+length, 0 );
   length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   length += PutIntParam( (INT32)(U_INT16)Attn1_Rdg, p_resp_parm+length, 0 );
   length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   length += PutIntParam( (INT32)(U_INT16)Mon_15Vpos_Rdg, p_resp_parm+length, 0 );
   length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   length += PutIntParam( (INT32)(U_INT16)Mon_15Vneg_Rdg, p_resp_parm+length, 0 );
   length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   length += PutIntParam( (INT32)(U_INT16)Mon_24V_Rdg, p_resp_parm+length, 0 );

   return length;
}

/***********************************************************************/
/* END FUNCTION: DoDTE1                                                */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: DoDTE2                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Return the latest raw thermal environment readings.        */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTE2( BIT8 *p_resp_parm )
{
   U_INT16  length;

   length = 0;

#if 0
   length += PutIntParam( (INT32)(U_INT16)Inlet_Mod_Temp, p_resp_parm+length, 0 );
   length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   length += PutIntParam( (INT32)(U_INT16)Inlet_Gas_ID, p_resp_parm+length, 0 );
   length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   length += PutIntParam( (INT32)(U_INT16)Det_Mod_Temp, p_resp_parm+length, 0 );
   length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   length += PutIntParam( (INT32)(U_INT16)Baro_Pres, p_resp_parm+length, 0 );
   length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   length += PutIntParam( (INT32)(U_INT16)Mon_10V_Rdg, p_resp_parm+length, 0 );
   length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   length += PutIntParam( (INT32)(U_INT16)Env_Offset, p_resp_parm+length, 0 );
   length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   length += PutIntParam( (INT32)(U_INT16)Aux_Mod_Temp, p_resp_parm+length, 0 );
   length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
#endif
   length += PutIntParam( (INT32)(U_INT16)Mon_5V_Rdg, p_resp_parm+length, 0 );
#if 0
   length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   length += PutIntParam( (INT32)(U_INT16)Env_Offset, p_resp_parm+length, 0 );
   length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );
   length += PutIntParam( (INT32)(U_INT16)Mon_24Vpneu_Rdg, p_resp_parm+length, 0 );
#endif

   return length;
}

/***********************************************************************/
/* END FUNCTION: DoDTE2                                                */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: DoDTRE                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Dumps the contents (and definition) of an EEPROM location  */
/*                                                                     */
/* RETURNED VALUE: a number and a string                               */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* CALLS:  the pneumatics generic command executor                     */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTRE(CMD_TYPE *p_cmd, BIT8 *p_resp_parm )
{
   INT32    i;
   UI_ERR   error;
   U_INT16  length;
   U_INT16  cmd[16];
   U_INT16  data;
   BIT8     def[32];

   error = OK;

   /* See if we have a pneumatics board installed */
   if ( IamAdirtball() )
   {
      CmdErrorHandler( p_cmd, 0, NOT_INSTALLED );
      return NO_RESPONSE;
   }

   for ( i = 1 ; i <= MIN(p_cmd->parmv[0],2) ; i++ )
   {
       if ( (error = GetAWordParam( p_cmd, i, &cmd[i-1] )) != OK ) break;
   }

   if ( p_cmd->parmv[0] != 2 )
   {
       error = INVALID_PARAM;
   }


   if ( error == OK )
   {
       if ( (error=GetEepromDefinition ( cmd[0], cmd[1], &data, def )) != OK )
       {
           CmdErrorHandler( p_cmd, 0, error );
           return NO_RESPONSE;
       }
   }
   else
   {
       CmdErrorHandler( p_cmd, i, error );
       return NO_RESPONSE;
   }

   length = 0;

   /* send back the value */

   length += PutIntParam( data,              p_resp_parm+length, 0 );
   length += PutAsciiParam( (BIT8 *)",",     p_resp_parm+length );

   /* send back the definition */

   length += PutAsciiParam( (BIT8 *)"\"",     p_resp_parm+length );
   length += PutAsciiParam( (BIT8 *)def,      p_resp_parm+length );
   length += PutAsciiParam( (BIT8 *)"\"",     p_resp_parm+length );

   return length;
}

/***********************************************************************/
/* END FUNCTION: DoDTRE                                                */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: DoDTWE                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: writes a value to the specified EEPROM location            */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* CALLS:  the pneumatics generic command executor                     */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTWE(CMD_TYPE *p_cmd )
{
   INT32    i;
   UI_ERR   error;
   U_INT16  cmd[3];

   /* See if we have a pneumatics board installed */
   if ( IamAdirtball() )
   {
      CmdErrorHandler( p_cmd, 0, NOT_INSTALLED );
      return NO_RESPONSE;
   }

   error = OK;

   for ( i = 1 ; i <= MIN(p_cmd->parmv[0],3) ; i++ )
   {
       if ( (error = GetAWordParam( p_cmd, i, &cmd[i-1] )) != OK ) break;
   }

   if ( p_cmd->parmv[0] != 3 )
   {
       error = INVALID_PARAM;
   }


   if ( error == OK )
   {
       if ( (WriteEeprom ( cmd[0], cmd[1], cmd[2] )) == FALSE )
       {
           CmdErrorHandler( p_cmd, 2, NOT_COMPATIBLE );
           return NO_RESPONSE;
       }
   }
   else
   {
       CmdErrorHandler( p_cmd, i, error );
       return NO_RESPONSE;
   }

   return NO_RESPONSE;
}


static INT16 DoDTPW(CMD_TYPE *p_cmd )
{
   INT32    i;
   UI_ERR   error;
   U_INT16  length;
   U_INT16  cmd[3];

   /* See if we have a pneumatics board installed */
   if ( IamAdirtball() )
   {
      CmdErrorHandler( p_cmd, 0, NOT_INSTALLED );
      return NO_RESPONSE;
   }

   error = OK;

   for ( i = 1 ; i <= MIN(p_cmd->parmv[0],3) ; i++ )
   {
       if ( (error = GetAWordParam( p_cmd, i, &cmd[i-1] )) != OK ) break;
   }

   if ( p_cmd->parmv[0] != 3 )
   {
       error = INVALID_PARAM;
   }


   if ( error == OK )
   {
       if ( (error=(UI_ERR)PrimitiveWriteEeprom ( cmd[0], cmd[1], cmd[2] )) != OK )
       {
           CmdErrorHandler( p_cmd, 0, error );
           return NO_RESPONSE;
       }
   }
   else
   {
       CmdErrorHandler( p_cmd, i, error );
       return NO_RESPONSE;
   }

   length = 0;

   return length;
}

/***********************************************************************/
/* END FUNCTION: DoDTWE                                                */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: DoDTCL                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Clear the diagnostic mode settings.                        */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* CALLS:  SetDiagMode                                                 */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTCL( BIT8 *p_resp_parm )
{
   return UIntToDec( (U_INT32)SetDiagMode(OFF), p_resp_parm );
}

/***********************************************************************/
/* END FUNCTION: DoDTCL                                                */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: DoDTAC                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Set the auto clear diagnostics on exit mode.               */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/* GLOBALS USED:  Disable_Diag_Auto_Clear                              */
/*                                                                     */
/* CALLS:  none                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTAC( CMD_TYPE  *p_cmd, BIT8 *p_resp_parm )
{
   U_INT8  param;

   param = p_cmd->parms[0];

   if ( param == '?' )
   {
      return UIntToDec((U_INT32)(INT32)Disable_Diag_Auto_Clear, p_resp_parm );
   }

   if ( param == '0' )
   {
      Disable_Diag_Auto_Clear = ON;
   }
   else
   {
      if ( param == '1' )
      {
         Disable_Diag_Auto_Clear = OFF;
      }
      else
      {
         CmdErrorHandler(p_cmd,1,INVALID_PARAM);
      }
   }

   return NO_RESPONSE;
}

/***********************************************************************/
/* END FUNCTION: DoDTAC                                                */
/***********************************************************************/



/***********************************************************************/
/* FUNCTION: DoDTAP                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Read current Atmospheric Pressure                          */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTAP( BIT8 *p_resp_parm )
{

   /* See if we have a pneumatics board installed */
   if ( IamAdirtball() )
   {
      return UIntToDec( 0, p_resp_parm );
   }
   else
   {
      return UIntToDec( (U_INT32)DcGetAtmosphericPressure(), p_resp_parm );
   }
}

/***********************************************************************/
/* END FUNCTION: DoDTAP                                                */
/***********************************************************************/



/***********************************************************************/
/* FUNCTION: DoDTBS                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Read current buffer status.                                */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTBS( BIT8 *p_resp_parm )
{
   U_INT16 len;
   U_INT16 i;

   len = 0;
   len += IntToDec( Buffer_Status[0], p_resp_parm+len );
   for( i = 1; i < BUF_STAT_LIM; i++)
   {
      len += PutAsciiParam((BIT8 *)",",p_resp_parm+len );
      len += IntToDec( Buffer_Status[i], p_resp_parm+len );
   }
   return len;
}

/***********************************************************************/
/* END FUNCTION: DoDTBS                                                */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: DoDTSX                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Get a single signal point from signal 1 or 2.              */
/*                                                                     */
/* RETURNED VALUE: length of response string                           */
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* CALLS:  none                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTSX( BIT8 *p_resp_parm, pSIG_TABLE psignal )
{
   INT16 len;
   BIT8  buf[11],*pbuf,*pbuf2;

   len   = 0;
   pbuf  = FormatDecimal( &buf[0], psignal->last_reading, 0, 11, 0 );

   /* move the data to the output buffer, removing leading blanks */
   for (pbuf2=&buf[0]; pbuf2<pbuf; pbuf2++)
   {
     if ( *pbuf2 != ' ' )
     {
        *(p_resp_parm+len) = *pbuf2;
        len++;
     }
   }

   len += PutAsciiParam( (BIT8 *)",",     p_resp_parm+len );
   len += PutIntParam( psignal->noise, p_resp_parm+len, 0 );

   return len;
}

/***********************************************************************/
/* END FUNCTION: DoDTSX                                                */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: DoDTEF                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Erase Flash buffer                                         */
/*                                                                     */
/* RETURNED VALUE: 0 = success, flash erased and buffer locked         */
/*                 1 = byte count exceeds size of flash buffer         */
/*                 2 = byte count too small or negative                */
/*                 3 = byte count is not a valid number                */
/*                10 = missing parameter, byte count is not optional   */
/*                14 = not allowed, buffer already locked by other host*/
/*                90 = success, buffer is now unlocked and available   */
/*                91 = failure, flash did not erase correctly          */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTEF( CMD_TYPE  *p_cmd, BIT8 *p_resp_parm )
{
  printf("call DoDTEF\n");
return 0;
// INT32   compressed_count;
// INT32   expanded_count;
// UI_ERR  error;
// 
//    if (p_cmd->parmv[0] >= 2)
//    {
//       error = DecToInt (&p_cmd->parms[p_cmd->parmv[1]], 0, &compressed_count);
//       if (error == OK)
//       {
//          error = DecToInt (&p_cmd->parms[p_cmd->parmv[2]], 0, &expanded_count);
//          if (error == OK)
//          {
//             error = EraseFlashBuffer (compressed_count, expanded_count, p_cmd->comm_port);
//          }
//       }
//    }
//    else
//    {
//       error = MISSING_PARAM;
//    }
//    return PutIntParam ((INT32)error, p_resp_parm, 0);
}

/***********************************************************************/
/* END FUNCTION: DoDTEF                                                */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: DoDTPF                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Program Flash buffer                                       */
/*                                                                     */
/* RETURNED VALUE: 0 = success, data programmed into flash buffer      */
/*                 3 = failure, invalid character in data              */
/*                10 = failure, no data                                */
/*                14 = not allowed, flash buffer not locked            */
/*                91 = flash did not program correctly                 */
/*                92 = data extends beyond end of buffer               */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTPF( CMD_TYPE  *p_cmd, BIT8 *p_resp_parm )
{
  printf("call DoDTPF\n");
  return 0;
// UI_ERR  error;
// 
//    if (p_cmd->parmv[0] > 0)
//    {
//       error = ProgramFlashBuffer(&(p_cmd->parms[p_cmd->parmv[1]]), p_cmd->comm_port);
//    }
//    else
//    {
//       error = MISSING_PARAM;
//    }
//    return PutIntParam ((INT32)error, p_resp_parm, 0);
}

/***********************************************************************/
/* END FUNCTION: DoDTPF                                                */
/***********************************************************************/

/***********************************************************************/
/* FUNCTION: DoDTSF                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Check Flash buffer & Prepare to Install                    */
/*                                                                     */
/* RETURNED VALUE: 0 = success, data validated and flag set            */
/*                14 = not allowed, flash buffer not locked            */
/*                93 = unrecognizable data in flash buffer             */
/*                94 = failure, corrupt or invalid data in flash buffer*/
/*                                                                     */
/* GLOBALS USED:  none                                                 */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

static INT16 DoDTSF( CMD_TYPE  *p_cmd, BIT8 *p_resp_parm )
{
  printf("call DoDTSF\n");
  return 0;
// UI_ERR  error;
// 
//    error = CheckFlashBuffer(p_cmd->comm_port);
//    return PutIntParam ((INT32)error, p_resp_parm, 0);
}

/***********************************************************************/
/* END FUNCTION: DoDTSF                                                */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: DTOpcodes                                                 */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: calls routines to implement the diagnostic test (DT)       */
/*                 commands.                                           */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* FILE SCOPE STATICS USED:                                            */
/*                                                                     */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/* WARNINGS:                                                           */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

INT16 DTOpcodes(CMD_TYPE  *p_cmd,BIT8  *p_resp_parm,U_INT16 max_resp_len)
{
INT16           parm_len;

   (void) max_resp_len;

   parm_len = NO_RESPONSE;
   switch( p_cmd->opcode.int_op )
   {
      case  MAKEINT('A','D'):
//        parm_len = DoCmd( p_cmd, p_resp_parm, &mDTAD );
        break;

      case  MAKEINT('A','P'):
        parm_len = DoDTAP( p_resp_parm );
        break;

      case  MAKEINT('B','S'):
        parm_len = DoDTBS( p_resp_parm );
        break;

      case  MAKEINT('C','L'):
        parm_len = DoDTCL( p_resp_parm );
        break;

      case  MAKEINT('A','C'):
        parm_len = DoDTAC( p_cmd, p_resp_parm );
        break;

      case  MAKEINT('C','M'):
        parm_len = DoDTCM( p_cmd, p_resp_parm );
        break;

      case  MAKEINT('C','P'):
 //       parm_len = DoCmd( p_cmd, p_resp_parm, &mDTCP );
        break;

      case  MAKEINT('D','S'):
  //      parm_len = DoStatus( p_cmd, p_resp_parm, &mDTDS );
        break;

      case  MAKEINT('D','A'):
        parm_len = DoDTDA( p_cmd, p_resp_parm );
        break;

      case  MAKEINT('D','F'):
  //      parm_len = DoCmd( p_cmd, p_resp_parm, &mDTDF );
        break;

      case  MAKEINT('D','B'):
 //       parm_len = DoCmd( p_cmd, p_resp_parm, &mDTDB );
        break;

      case  MAKEINT('E','F'):
        parm_len = DoDTEF( p_cmd, p_resp_parm );
        break;

      case  MAKEINT('E','O'):
        parm_len = DoDTEO(p_cmd,p_resp_parm);
        break;

      case  MAKEINT('E','N'):
        parm_len = DoDTEN( p_resp_parm );
        break;

      case  MAKEINT('E','V'):
        parm_len = DoDTEV( p_resp_parm );
        break;

      case  MAKEINT('E','1'):
        parm_len = DoDTE1( p_resp_parm );
        break;

      case  MAKEINT('E','2'):
        parm_len = DoDTE2( p_resp_parm );
        break;

      case  MAKEINT('H','S'):
  //      parm_len = DoCmd( p_cmd, p_resp_parm, &mDTHS );
        break;

      case  MAKEINT('I','V'):
        parm_len = DoCmd( p_cmd, p_resp_parm, &mDTIV );
        break;

      case  MAKEINT('I','F'):
        parm_len = DoStatus( p_cmd, p_resp_parm, &mDTIF );
        break;

      case  MAKEINT('I','O'):
        parm_len = DoDTIO( p_cmd, p_resp_parm );
        break;

      case  MAKEINT('I','B'):
        parm_len = DoStatus( p_cmd, p_resp_parm, &mDTIB );
        break;

      case  MAKEINT('M','D'):
        parm_len = DoDTMD( p_cmd, p_resp_parm );
        break;

      case  MAKEINT('M','I'):
        parm_len = DoDTMI( p_cmd, p_resp_parm );
        break;

      case  MAKEINT('M','T'):
        parm_len = DoDTMT( p_resp_parm );
        break;

      case  MAKEINT('M','X'):
        parm_len = DoDTMX( p_cmd, p_resp_parm );
        break;

      case  MAKEINT('O','V'):
  //      parm_len = DoCmd( p_cmd, p_resp_parm, &mDTOV );
        break;

      case  MAKEINT('P','D'):
        parm_len = DoCmd( p_cmd, p_resp_parm, &mDTPD );
        break;

      case  MAKEINT('P','F'):
        parm_len = DoDTPF( p_cmd, p_resp_parm );
        break;

      case  MAKEINT('P','K'):
        parm_len = DoDTPK( p_cmd, p_resp_parm );
        break;

      case  MAKEINT('P','N'):
        parm_len = DoDTPN( p_resp_parm );
        break;

      case  MAKEINT('P','O'):
        parm_len = DoStatus( p_cmd, p_resp_parm, &mDTPO );
        break;

      case  MAKEINT('P','R'):
        parm_len = DoDTPR( p_resp_parm );
        break;

      case  MAKEINT('P','S'):
        parm_len = DoDTPS( p_resp_parm );
        break;

      case  MAKEINT('P','V'):
        parm_len = DoDTPV( p_resp_parm );
        break;

      case  MAKEINT('P','W'):
        parm_len = DoDTPW( p_cmd );
        break;

      case  MAKEINT('R','E'):
        parm_len = DoDTRE( p_cmd, p_resp_parm );
        break;

      case  MAKEINT('R','V'):
        parm_len = DoDTRV( p_resp_parm );
        break;

      case  MAKEINT('R','D'):
        parm_len = DoDTRD( p_resp_parm );
        break;

      case  MAKEINT('S','1'):
        parm_len = DoDTSX( p_resp_parm, pSIGNAL1 );
        break;

      case  MAKEINT('S','2'):
        parm_len = DoDTSX( p_resp_parm, pSIGNAL2 );
        break;

      case  MAKEINT('S','C'):
        parm_len = DoDTSC( p_cmd, p_resp_parm );
        break;

      case  MAKEINT('S','F'):
        parm_len = DoDTSF( p_cmd, p_resp_parm );
        break;

      case  MAKEINT('S','K'):
        parm_len = DoDTSK();
        break;

      case  MAKEINT('S','N'):
        parm_len = DoDTSN( p_cmd, p_resp_parm );
        break;

      case  MAKEINT('S','P'):
        parm_len = DoDTSP( p_cmd, p_resp_parm );
        break;

      case  MAKEINT('S','T'):
        parm_len = DoDTST( p_cmd, p_resp_parm );
        break;

      case  MAKEINT('W','E'):
        parm_len = DoDTWE( p_cmd );
        break;

      case  MAKEINT('X','P'):
        parm_len = DoDTXP( p_cmd, p_resp_parm );
        break;

      case  MAKEINT('X','R'):
        parm_len = DoDTXR( p_cmd, p_resp_parm );
        break;

      case  MAKEINT('Z','C'):
 //       parm_len = DoCmd( p_cmd, p_resp_parm, &mDTZC );
        break;

      case  MAKEINT('Z','D'):
 //       parm_len = DoCmd( p_cmd, p_resp_parm, &mDTZD );
        break;

      case  MAKEINT('Z','F'):
        parm_len = DoDTZF( p_cmd );
        break;

      case  MAKEINT('Z','G'):
        parm_len = DoDTZG( p_cmd, p_resp_parm );
        break;

      case  MAKEINT('Z','L'):
 //       if ( pZone_Tune == &Oven_Table ) parm_len = DoStatus( p_cmd, p_resp_parm, &mDTZL );
 //       else parm_len = DoCmd( p_cmd, p_resp_parm, &mDTZL );
        break;

      case  MAKEINT('Z','N'):
 //       parm_len = DoCmd( p_cmd, p_resp_parm, &mDTZN );
        break;

      case  MAKEINT('Z','P'):
        parm_len = DoDTZP( p_resp_parm );
        break;

      case  MAKEINT('Z','R'):
 //       parm_len = DoCmd( p_cmd, p_resp_parm, &mDTZR );
        break;

      case  MAKEINT('Z','T'):
 //       if ( pZone_Tune == &Oven_Table ) parm_len = DoStatus( p_cmd, p_resp_parm, &mDTZT );
 //       else parm_len = DoCmd( p_cmd, p_resp_parm, &mDTZT );
        break;

      default:
         parm_len = NO_RESPONSE;
         CmdErrorHandler( p_cmd, 0, INVALID_OP );
         break;
   }
   return parm_len;
}


/***********************************************************************/
/* END FUNCTION: DTOpcodes                                             */
/***********************************************************************/




/***********************************************************************/
/* FUNCTION: DT_Cmds                                                   */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: calls function which executes a command if one is          */
/*          available on the command queue.                            */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/* GLOBALS USED:                                                       */
/*                                                                     */
/* FILE SCOPE STATICS USED:                                            */
/*                                                                     */
/*                                                                     */
/* CALLS:                                                              */
/*                                                                     */
/* LIMITATIONS:                                                        */
/*                                                                     */
/* WARNINGS:                                                           */
/*                                                                     */
/*                                                                     */
/***********************************************************************/

void DT_Cmds(void)
{
   ExecCmds(&DT_Cmd_Q,DTOpcodes);
}

/***********************************************************************/
/* END FUNCTION: DT_Cmds                                               */
/***********************************************************************/

